package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.dav.bizapi.service.base.vo.HSBCPSVO;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.SchemeColorBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.result.SchemeColorListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.result.SchemeColorListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.SchemeColorForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.SchemeColorIdForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.SchemeColorUpdateForm;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;
import com.cloudbroker.bcs.platform.dav.middle.entity.SchemeColor;
import com.cloudbroker.bcs.platform.dav.middle.service.SchemeColorService;

/**
 * Created by zhaojp18008 on 2016/5/18.
 */
@Service
public class SchemeColorBizServiceImpl implements SchemeColorBizService {
    @Resource
    private SchemeColorService schemeColorService;
    @Override
    public SchemeColorListResult funcSchemeColorListGet(HSBCPSVO param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT,SchemeColorListResult.class);
        }
        List<SchemeColor> schemeColorList = this.schemeColorService.query(new SchemeColor());
        List<SchemeColorListData> schemeColorListDataList = new ArrayList<>();
        for(SchemeColor schemeColor:schemeColorList)
        {
            SchemeColorListData schemeColorListData = new SchemeColorListData();
            schemeColorListData.setSchemecolor_id(schemeColor.getSchemecolor_id());
            schemeColorListData.setSchemecolor_name(schemeColor.getSchemecolor_name());
            schemeColorListData.setSchemecolor_value(schemeColor.getSchemecolor_value());
            schemeColorListDataList.add(schemeColorListData);

        }
        SchemeColorListResult schemeColorListResult = new SchemeColorListResult();
        schemeColorListResult.setData_list(schemeColorListDataList);
        return VOUtil.setSuccessResult(schemeColorListResult);
    }

    @Override
    public HSBCPSBackVO funcSchemeColorAdd(SchemeColorForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT,SchemeColorListResult.class);
        }
        SchemeColor schemeColor = new SchemeColor();
        schemeColor.setSchemecolor_value(param.getSchemecolor_value());
        schemeColor.setSchemecolor_name(param.getSchemecolor_name());
        schemeColor.setCreate_time(new Date());
        schemeColorService.save(schemeColor);
        return VOUtil.genSuccessResultVo();
    }

    @Override
    public HSBCPSBackVO funcSchemeColorUpdate(SchemeColorUpdateForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT,SchemeColorListResult.class);
        }

        if(schemeColorService.get(String.valueOf(param.getSchemecolor_id())) == null)
        {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.SchemeColorErr.NO_SCHEME_COLOR,SchemeColorListResult.class);
        }
        SchemeColor schemeColor = new SchemeColor();
        schemeColor.setSchemecolor_value(param.getSchemecolor_value());
        schemeColor.setSchemecolor_name(param.getSchemecolor_name());
        schemeColor.setSchemecolor_id(Long.valueOf(param.getSchemecolor_id()));
        schemeColorService.update(schemeColor);

        return VOUtil.genSuccessResultVo();
    }

    @Override
    public HSBCPSBackVO funcSchemeColorDel(SchemeColorIdForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT,SchemeColorListResult.class);
        }

        if(schemeColorService.get(String.valueOf(param.getSchemecolor_id())) == null)
        {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.SchemeColorErr.NO_SCHEME_COLOR,SchemeColorListResult.class);
        }
        schemeColorService.delete(String.valueOf(param.getSchemecolor_id()));

        return VOUtil.genSuccessResultVo();
    }
}
